<?php
class ClassificationsController extends AppController {

	public $name = 'Classifications';
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('*');
		$this->setLayout();
	}

	function admin_index() {
		$this->Classification->recursive = 0;
		$this->set('classifications', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Classification->create();
			if ($this->Classification->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'classification'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'classification'));
			}
		}else{
			$this->set('parent_id', $this->Classification->find('list', array('conditions' => array('Classification.parent_id' => 0), 'recursive' => -1)));
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'classification'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Classification->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'classification'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'classification'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Classification->read(null, $id);
			$this->set('parent_id', $this->Classification->find('list', array('conditions' => array('Classification.parent_id' => 0), 'recursive' => -1)));
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid id for %s', true), 'classification'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Classification->delete($id)) {
			$this->Session->setFlash(sprintf(__('%s deleted', true), 'Classification'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(sprintf(__('%s was not deleted', true), 'Classification'));
		$this->redirect(array('action' => 'index'));
	}
        public function childClassification($parentId){
            $children =  $this->Classification->find('list', array('conditions' => array('Classification.parent_id' => $parentId), 'recursive' => -1));
            $this->set('children',$children);
            
        }
}
?>